/**
 * Created by lxh on 2018/1/2.
 */
import React,{PureComponent}from 'react';
import {connect} from 'dva';
import {Form, Input, Button, Radio, Checkbox, Modal, message,Table} from 'antd';

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const CheckboxGroup = Checkbox.Group;
@connect(state => ({
    adminPowerSetting: state.adminPowerSetting,
}))
export default class AdminPowerEdit extends PureComponent {
    state={
        selectedRowKeys: [],
        user_id:'',//管理员id
        seller_id:'',
        functionType:'',//权限类型
    }
    componentWillReceiveProps(nextProps) {
        // clean state
        if (nextProps.selectedRows.length === 0) {
            this.setState({
                selectedRowKeys: [],
            });
        }else{
            this.setState({
                selectedRowKeys:nextProps.selectedRows,
            })
        }
    }
    handleRowSelectChange = (selectedRowKeys, selectedRows) => {
        if (this.props.onSelectRow) {
            this.props.onSelectRow(selectedRowKeys);
        }
        this.setState({ selectedRowKeys });
    }

    componentDidMount(){
        const {userId,sellerId,actionType,functionType}=this.props;
        this.setState({
            user_id: userId,
            seller_id: sellerId,
            actionType: actionType,
            functionType: functionType,
        })
    }
    handleTableChange=(pagination)=>{
        const {dispatch} = this.props;
        const params = {
            currentPage: pagination.current,
            pageSize: pagination.pageSize,
            userId:this.state.user_id,
            actionType:this.state.actionType,
        };
        dispatch({
            type: 'adminPowerSetting/queryPowerList',
            payload: params,
        });
    }
    handleSubmit=()=>{
        const {user_id,functionType}=this.state;
        this.props.dispatch({
            type:'adminPowerSetting/updateAdminPower',
            payload:{actionList:this.state.selectedRowKeys,
                user_id: user_id,
                seller_id: this.props.sellerId,
                functionType: functionType,
                mod_user_id:JSON.parse(localStorage.getItem("userInfoState")).userInfo.user_id,},

            callback:(response)=>{
                if(response.result===0) {
                    this.props.handleCancel();

                    message.success("提交成功")
                    // this.props.dispatch({
                    //     type: 'adminPowerSetting/queryAdminPowerList',
                    //     payload:{userId:this.state.user_id}
                    // })
                }else{
                    message.error("提交失败")
                }
            }
        })
    }
    handleCancel=()=>{
        this.props.handleCancel();
    }
    render() {
        const { selectedRowKeys } = this.state;
        const {powerData: {list, pagination}} = this.props.adminPowerSetting;
        const columns = [{
            title: '权限ID',
            dataIndex: 'ACTION_ID',
            key: 'ACTION_ID',
        },
            {
                title: '权限编码',
                dataIndex: 'ACTION_CODE',
                key: 'action_code',
            }, {
                title: '权限说明',
                dataIndex: 'ACTION_DESC',
                key: 'action_desc',
            }, {
                title: '权限类型',
                dataIndex: 'action_type',
                key: 'action_type',
            },
        ];
        const paginationProps = {
            showSizeChanger: true,
            showQuickJumper: true,
            ...pagination,
        };
        const rowSelection = {
            selectedRowKeys,
            onChange: this.handleRowSelectChange,
        };

        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 7},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 10},
            },
        };

        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };
        return (
        <div>
            <div>
                <Table
                    columns={columns}
                    dataSource={list}
                    rowKey={record => record.ACTION_ID}
                    pagination={paginationProps}
                    rowSelection={rowSelection}
                    onChange={this.handleTableChange}
                />
                <div align="center">
                    <Button type="primary" onClick={this.handleSubmit}>提交</Button>
                    <Button type="danger" onClick={this.handleCancel}>取消</Button>
                </div>
            </div>

        </div>
        );
    }
}


